<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\BusinessException;
use App\Models\GoldModel;
use App\Models\OrderGoodsModel;
use App\Models\OrderModel;
use App\Models\OrderRefundModel;
use App\Utils\ResponseEnum;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class RefundController extends ApiController
{
    /**
     * 申请退款或退货退款
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function doSave(Request $request): JsonResponse
    {
        $uid = $this->verifyId('uid');
        $orderNo = $this->verifyString('order_no');
        $order = OrderModel::where('order_no', $orderNo)->where('uid', $uid)->first();
        if (!$order || $order->pay_status == 0)
            return $this->fail(ResponseEnum::CLIENT_DELETED_ERROR);
        if ($order->refund_status > 0)
            return $this->fail(ResponseEnum::SYSTEM_ERROR);
        if ($order->done_status == 0) {
            $refundGold = $order->goods_total + $order->express_fee + $order->daifa_fee + $order->check_fee + $order->package_fee
                + $order->brand_fee + $order->wishcard_fee;
            GoldModel::createGold($orderNo, $uid, $refundGold, '退款', 0);
        }
        $this->createRefundOrder($uid, $orderNo, $order->done_status ? 1 : 3);
        $order->refund_status = 1;
        $order->save();
        #todo 推送聚水潭
        return $this->success();
    }

    /**
     * 处理用户退款/退款
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function doRefund(Request $request): JsonResponse
    {
        $status = $this->verifyInteger('status');
        $orderNo = $this->verifyString('order_no');
        $refundOrder = OrderRefundModel::where('order_no', $orderNo)->orderByDesc('status')->first();
        if (!$refundOrder)
            return $this->fail(ResponseEnum::CLIENT_DELETED_ERROR);
        if ($refundOrder->status >= 4)
            return $this->fail(ResponseEnum::SYSTEM_ERROR);
        $this->createRefundOrder($refundOrder->uid, $orderNo, $status);
        if ($status == 4)
            $this->doAddUserGold($refundOrder->uid, $orderNo, $request->input('gold'));
        OrderModel::where('order_no', $orderNo)->update(['finish_status' => 1]);
        return $this->success();
    }

    /**
     * 更新退单流程反馈
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function doUpdate(Request $request): JsonResponse
    {
        $id = $this->verifyId('id');
        $orderRefund = OrderRefundModel::where('id', $id)->first();
        if ($orderRefund) {
            $orderRefund->remark = $request->input('remark');
            $orderRefund->response = $request->input('response');
            $orderRefund->save();
        }
        return $this->success();
    }

    /**
     * 补充退单快递信息
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function saveExpress(Request $request): JsonResponse
    {
        $orderNo = $this->verifyString('order_no');
        $refundOrder = OrderRefundModel::where('order_no', $orderNo)->orderByDesc('status')->first();
        if ($refundOrder)
            return $this->fail(ResponseEnum::CLIENT_DELETED_ERROR);
        $this->createRefundOrder($refundOrder->uid, $orderNo, 3);
        return $this->success();
    }

    /**
     * 取消退款
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function cancelRefund(Request $request): JsonResponse
    {
        $orderNo = $this->verifyString('order_no');
        $order = OrderModel::where('order_no', $orderNo)->where('refund_status', 1)->first();
        $order->refund_status = 0;
        $order->save();
        OrderRefundModel::where('order_no', $orderNo)->delete();
        GoldModel::where('order_no', $orderNo)->delete();
        #todo 推送聚水潭
        return $this->success($order);
    }

    /**
     * 处理部分退货
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function doRefundGoods(Request $request): JsonResponse
    {
        $id = $this->verifyId('id');
        $orderGoods = OrderGoodsModel::where('id', $id)->first();
        if (!$orderGoods)
            return $this->fail(ResponseEnum::CLIENT_DELETED_ERROR);
        if ($orderGoods->refund > 0)
            return $this->fail(ResponseEnum::SYSTEM_ERROR);
        $order = OrderModel::where('order_no', $orderGoods->order_no)->where('pay_status', 1)->where('refund_status', 0)->first();
        if ($order) {
            GoldModel::createGold($order->order_no . $id, $order->uid, $request->input('gold', 0), '部分退货', 1);
            $orderGoods->refund = 1;
            $orderGoods->save();
            $order->refund_status = 2;
            $order->save();
        }
        #todo 推送聚水潭
        return $this->success();
    }

    /**
     * 创建退款流程
     * @param $uid
     * @param $orderNo
     * @param $status
     * @return void
     */
    private function createRefundOrder($uid, $orderNo, $status): void
    {
        OrderRefundModel::create([
            'order_no'      => $orderNo,
            'uid'           => $uid,
            'status'        => $status,
            'refund_remark' => OrderRefundModel::getRefundStatusText($status),
            'refund_express_no' => \request()->input('express_no'),
            'refund_express_company' => \request()->input('company'),
        ]);
    }

    /**
     * 执行新增用户金币
     * @param $uid
     * @param $orderNo
     * @param $gold
     * @return void
     */
    private function doAddUserGold($uid, $orderNo, $gold): void
    {
        $goldData = GoldModel::where('order_no', $orderNo)->where('gold', '>', 0)->first();
        if (!$goldData)
            $goldData = GoldModel::createGold($orderNo, $uid, $gold, '退款', 0);
        if (!empty($gold))
            $goldData->gold = $gold;
        $gold->status = 1;
        $gold->save();
    }
}
